package com.ruoyi.web.controller.wx.points;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.page.PageUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.domain.PoiApply;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.domain.wx.AppQueryVo;
import com.ruoyi.web.mapper.PoiApplyMapper;
import com.ruoyi.web.service.IPoiApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/wx/wxPoiApply")
public class WxPoiApplyController extends BaseController {

    @Autowired
    private IPoiApplyService poiApplyService;

    @Autowired
    private PoiApplyMapper poiApplyMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @PostMapping("/getApplyListByUser")
    public TableDataInfo getApplyListByUser(@RequestBody AppQueryVo appQueryVo) {

        if(appQueryVo.getQUserId() == null){
            appQueryVo.setQUserId(SecurityUtils.getUserId());
        }
        appQueryVo.setIsAsc("ascending");
        appQueryVo.setOrderByColumn("id");
        PageUtils page = poiApplyService.appListByPage(appQueryVo);
        return getDataTable(page);

    }


    @PostMapping("/getApplyList")
    public AjaxResult getApplyList(@RequestBody AppQueryVo appQueryVo) {

        Date date = new Date();
        QueryWrapper<PoiApply> qwPoiApply = new QueryWrapper<>();
        qwPoiApply.eq("year",DateUtils.getYear(date));
        qwPoiApply.eq("quarter",DateUtils.getQuarter(date));
        if(appQueryVo.getQStatus()!=null){
            qwPoiApply.ge("apply_status",appQueryVo.getQStatus());
            qwPoiApply.le("apply_status",appQueryVo.getQStatus()+5);
        }

        List<PoiApply> poiApplies = poiApplyMapper.selectList(qwPoiApply);

        return success(poiApplies);

    }

    @PreAuthorize("@ss.hasPermi('points:poiManagement:list')")
    @GetMapping("/getManagementList")
    public TableDataInfo getManagementList(QueryVo queryVo) {
        SysUser sysUser = new SysUser();
        sysUser.setNickName(queryVo.getQKey());
        if(queryVo.getQIds1()!=null && !StringUtils.isEmpty(queryVo.getQIds1())){
            sysUser.setDeptId(queryVo.getQIds1().get(queryVo.getQIds1().size()-1));
        }
        sysUser.setStatus("0");
        List<SysUser> list = sysUserMapper.selectUserList(sysUser);
        return getDataTable(list);
    }

}

